package com.jeesite.modules.car.web;

import com.alibaba.fastjson.JSONObject;
import com.jeesite.common.config.Global;
import com.jeesite.common.entity.Page;
import com.jeesite.common.lang.StringUtils;
import com.jeesite.common.web.BaseController;
import com.jeesite.modules.car.entity.Brand;
import com.jeesite.modules.car.entity.Car;
import com.jeesite.modules.car.entity.CarType;
import com.jeesite.modules.car.entity.Image;
import com.jeesite.modules.car.service.BrandService;
import com.jeesite.modules.car.service.CarTypeService;
import com.jeesite.modules.general.CosUtil;
import com.jeesite.modules.sys.utils.RoleUtils;
import com.jeesite.modules.sys.utils.UserUtils;
import org.apache.commons.io.FilenameUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

import static com.jeesite.common.web.BaseController.text;
import static com.jeesite.common.web.http.ServletUtils.renderResult;

@Controller
@RequestMapping(value = "${adminPath}/brand/brand")
public class BrandController extends BaseController {

    @Autowired
    private BrandService brandService;
    @Autowired
    private CarTypeService carTypeService;

    @ModelAttribute
    public Brand get(String brandId, boolean isNewRecord) {
        return brandService.get(brandId, isNewRecord);
    }

    /**
     * 查询列表
     */
    @RequiresPermissions("brand:brand:view")
    @RequestMapping(value = {"list", ""})
    public String list(Brand brand, Model model) {
        model.addAttribute("brand", brand);
        return "modules/brand/brandList";
    }

    /**
     * 查看编辑表单
     */
    @RequiresPermissions("brand:brand:view")
    @RequestMapping(value = "form")
    public String form(Brand brand, Model model) {
        model.addAttribute("brand", brand);
        model.addAttribute("baseUrl", CosUtil.URL);
        return "modules/brand/brandForm";
    }

    /**
     * 查询列表数据
     */
    @RequiresPermissions("brand:brand:view")
    @RequestMapping(value = "listData")
    @ResponseBody
    public Page<Brand> listData(Brand brand, HttpServletRequest request, HttpServletResponse response) {
        brand.setPage(new Page<>(request, response));
        Page<Brand> page = brandService.findPage(brand);
        return page;
    }

    /**
     * 新增品牌数据
     * @param files
     * @param brand
     * @return
     */
    @RequiresPermissions("brand:brand:edit")
    @PostMapping(value = "saveBrand")
    @ResponseBody
    public String saveBrand(@RequestParam(value = "files", required = false) MultipartFile[] files, @Validated Brand brand,String delBrand) {
        if(StringUtils.isNotEmpty(delBrand)){
            CosUtil.delCOSFile(delBrand, "brand");
        }

        if (!Objects.isNull(files)) {
            for (MultipartFile file : files) {
                String[] name = (FilenameUtils.getName(file.getOriginalFilename())).split("_");
                if("cover".equals(name[0])){
                    JSONObject result = CosUtil.uploadFile(file, "", "brand");
                    if(!result.getBoolean("result")){
                        brand.setLogo(result.getString("fileName2"));
                    }else {
                        return renderResult(Global.TRUE, text("品牌logo上传失败！"));
                    }
                }
            }
            if (brand.getBrandId() != null && !brand.getBrandId().equals("")){  //修改
                brandService.update(brand);
            }else{  // 新增
                brandService.insert(brand);
            }

            return renderResult(Global.TRUE, text("保存数据成功！"));
        } else {
            return renderResult(Global.FALSE, text("图片未上传！"));
        }
    }

    /**
     * 删除数据  物理删除
     */
    @RequiresPermissions("brand:brand:edit")
    @RequestMapping(value = "deleteRowData")
    @ResponseBody
    public String deleteRowData(Brand brand) {
        if (brand.getBrandId() != null && !brand.getBrandId().equals("")){
            brandService.delBrandData(brand.getBrandId());
            return renderResult(Global.TRUE, text("操作成功！"));
        }
        return renderResult(Global.FALSE, text("系统错误！"));
    }

    @RequestMapping("findList")
    @ResponseBody
    public List<Brand> findList(Brand brand){
        CarType carType = new CarType();
        carType.setTypeCode(brand.getTypeCode());
        CarType carType1 = carTypeService.get(carType);
        brand.setType(carType1.getBigType());
        return brandService.findList(brand);
    }

}
